package com.itheima.service;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.vo.OrderInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service(interfaceClass =OrderService.class )
@Transactional
public class OrderServiceImpl implements OrderService {


    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderSettingDao orderSettingDao;
    @Autowired
    private OrderDao orderDao;

    @Override
    public Result submit(OrderInfoVo orderInfoVo) {
        String name = orderInfoVo.getName();
        String validateCode = orderInfoVo.getValidateCode();
        String telephone = orderInfoVo.getTelephone();
        Date orderDate = orderInfoVo.getOrderDate();
        Integer setmealId = orderInfoVo.getSetmealId();
        String orderType = orderInfoVo.getOrderType();
        String idCard = orderInfoVo.getIdCard();
        Integer sex = orderInfoVo.getSex();

        //1.查看今天是否有预约设置
        OrderSetting orderSetting=orderSettingDao.findByDate(orderDate);
        if (null==orderSetting){
            return Result.error(MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        int number = orderSetting.getNumber();
        int reservations = orderSetting.getReservations();

        //2.查看今天是否预约已满
        if (number<=reservations){
            return Result.error(MessageConstant.ORDER_FULL);

        }
        //3.收集用户信息
        Member member = memberDao.findByTelephone(telephone);
        if (null==member){
             member = new Member();
             member.setName(name);
             member.setSex(sex.toString());
             member.setPhoneNumber(telephone);
             member.setRegTime(new Date());
             member.setIdCard(idCard);
             memberDao.add(member);
        }
        Integer memberId = member.getId();
        //4.检查当前用户，当前日期，当前套餐是否存在预约
        Order queryOrder=new Order();
        queryOrder.setMemberId(memberId);
        queryOrder.setOrderDate(orderDate);
        queryOrder.setSetmealId(setmealId);
        List<Order> orders = orderDao.findByCondition(queryOrder);
        if (CollectionUtil.isNotEmpty(orders)){
            return Result.error(MessageConstant.HAS_ORDERED);
        }
        //5.创建订单（未支付）
        Order order = new Order();
        order.setMemberId(memberId);
        order.setOrderDate(orderDate);
        order.setSetmealId(setmealId);
        order.setOrderType(orderType);
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        orderDao.add(order);
        //6.修改预约人数
        //orderSetting.getVersion()  乐观锁
        int count=orderSettingDao.updateReservations(orderDate,orderSetting.getVersion());
        if (count!=1){
            throw new RuntimeException("预约失败");
        }

        return Result.success(MessageConstant.ORDER_SUCCESS,order);
    }

    @Override
    public Map findById(Integer id) {
        Map map = orderDao.findById4Detail(id);
        return map;
    }
}
